import java.util.Scanner;

/**
 * 练习题 1.1
 * @author Zhang Yiming
 * @since 2018.3.6
 */

public class First {

    public static void main(String[] args) {
	    // write your code here
//        System.out.println(2.0e-6 * 100000000.1);

//        System.out.println(1 + 2 + "3");

//        int a, b, c;
//        a = 0; b = 1;
//        if ( a > b) c = 0; else b = 0;

        First first = new First();
//        first.oneOneThree();
//        first.oneOneSix();
//        first.oneOneSeven();
//        first.oneOneNine();
//        first.oneOneEleven();
//        first.oneOneTwelve();
//        first.oneOneThirteen();
//        System.out.println(first.oneOneFourteen(5));
//        int[] a1 = {1,2,3,5,4,5,4,5, 4,2, 4};
//        for (int i: first.histogram(a1) ) {
//            System.out.println(i);
//        }
        System.out.println(first.exR1(6));

    }


    // 1.1.3
    public void oneOneThree() {
        Scanner reader = new Scanner(System.in);

        System.out.print("Please input first number:");
        int first = reader.nextInt();

        System.out.print("Please input second number:");
        int second = reader.nextInt();

        System.out.print("Please input third number:");
        int third = reader.nextInt();

        if ( first == second && second == third ) {
            System.out.println("equal");
        } else {
            System.out.println("not equal");
        }
    }

    // 1.1.6
    public void oneOneSix() {
        int f = 0;
        int g = 1;
        for (int i=0; i < 15; i++) {
            System.out.println(f);
            f = f + g;
            g = f - g;
        }
    }

    // 1.17
    public void oneOneSeven() {
        // a
        double t = 9.0;
        while (Math.abs(t - 9.0/t) > 0.001) {
            t = (9.0/t + t) / 2.0;
        }
        System.out.printf("%.5f\n", t);

        // b
        int sum = 0;
        for(int i=1; i< 1000; i++) {
            for(int j = 0; j < i; j++) {
                sum++;
            }
        }

        System.out.println(sum);

        // c
        sum = 0;
        for(int i=1; i< 1000; i *= 2) {
            for(int j = 0; j < 1000; j++) {
                sum++;
            }
        }

        System.out.println(sum);
    }

    // 1.1.9 将整数转化成二进制
    public void oneOneNine() {
        Scanner scanner = new Scanner(System.in);
        String s = "";

        System.out.println("Please input a number");
        int N = scanner.nextInt();

        for( int i=N; i > 0; i /=2 ) {
            s = (i % 2) + s;
        }

        System.out.println(s);
        System.out.println(Integer.toBinaryString(N));
    }

    // 1.1.11
    public void oneOneEleven() {
        boolean array[][] = {{true, false, true}, {true, true, false}, {false, false, true}};

        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[i].length; j++) {
                if ( array[i][j] ) {
                    System.out.printf("Row: %d; Col: %d; value is: %s \n", i, j, "*");
                    continue;
                }
                System.out.printf("Row: %d; Col: %d; value is: %s \n", i, j, "");
            }
        }
    }

    // 1.1.12
    public void oneOneTwelve() {
        int[] a = new int[10];
        for (int i = 0; i < 10; i++) {
            a[i] = 9 - i;
        }
        for (int i = 0; i < 10; i++) {
            a[i] = a[a[i]];
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }

    }

    // 1.1.13
    public void oneOneThirteen() {
        int[][] a = {{1,2,3}, {4,5,6}, {7,8,9}};

        System.out.println("Normal");
        for(int[] i: a) {
            for(int j: i) {
                System.out.printf("%d\t", j);
            }
            System.out.println();
        }

        System.out.println("Transpose");
        for(int i = 0; i < a[0].length; i++) {
            for(int j = 0; j < a.length; j++) {
                System.out.printf("%d\t", a[j][i]);
            }
            System.out.println();
        }
    }

    // 1.1.14
    private double innerPow(double base, double pow) {
        if(pow == 0) {
            return 1;
        }
        double v = innerPow(base, pow / 2);

        if ((int) pow % 2 == 0) {
            return v * v;
        } else {
            return v * v * base;
        }
    }

    private double myPow(double base, double pow) {
        if (pow < 0) {
            return 1 / innerPow(base, -pow);
        } else {
          return innerPow(base, pow);
        }
    }

    public int oneOneFourteen(int N) {
        int i = 0;
        while (N > myPow(2, i)) {
            i++;
        }
        return i - 1;
    }

    // 1.1.15
    private int max(int[] a) {
        int result = 0;
        for (int i: a) {
            if (i > result) {
                result = i;
            }
        }
        return result;
    }

    public int[] histogram(int[] a) {
        int maxValue = max(a);
        int result[] = new int[maxValue + 1];
        for(int i = 0; i < a.length; i++) {
            if (a[i] <= maxValue) {
                result[a[i]]++;
            }
        }
        return result;
    }

    // 1.1.16
    public String exR1(int x) {
        if (x <= 0) return "";
        return exR1(x-3) + x + exR1(x-2) + x;
    }
}
